﻿using DG.Tweening;
using GameFramework;
using Sirenix.OdinInspector;
using System;
using System.Collections;
using System.Linq;
using TowerDefence_Zombie.Data;
using UnityEngine;
using UnityEngine.UI;
using UnityGameFramework.Runtime;

namespace TowerDefence_Zombie
{
    public class UITreasureForm : UGuiFormEx
    {
        [Title("开始界面")] public GameObject StartPanel;
        [Title("开始界面宝箱")] public Image StartTreasureImage;

        [Title("动画界面")] public GameObject AnimatePanel;
        [Title("动画界面宝箱")] public Image AnimateTreasureImage;
        [Title("动画卡片")] public TreasureProItem treasureProItem;

        [Title("结束界面")] public GameObject EndPanel;
        [Title("结束界面卡片节点")] public Transform EndCardNode;
        [Title("结束界面关闭按钮")] public Button EndCloseButton;

        [Title("卡片预制体")] public GameObject CardPrefab;

        private UITreasureFormOpenParam uIGameOverFormOpenParam;

        private Coroutine m_ProcessTreasureCoroutine;
        protected override void OnInit(object userData)
        {
            base.OnInit(userData);
            EndCloseButton.onClick.AddListener(CloseForm);
        }

        protected override void OnOpen(object userData)
        {
            base.OnOpen(userData);
            uIGameOverFormOpenParam = userData as UITreasureFormOpenParam;
            StartTreasureImage.sprite = Resources.Load<Sprite>(uIGameOverFormOpenParam.TreasureBoxData.IconResId);
            AnimateTreasureImage.sprite = Resources.Load<Sprite>(uIGameOverFormOpenParam.TreasureBoxData.OpenIconRes);
            if (uIGameOverFormOpenParam == null)
            {
                Log.Error("UIGameOverForm open param tyoe invaild");
                return;
            }
        }

        protected override void OnUpdate(float elapseSeconds, float realElapseSeconds)
        {
            base.OnUpdate(elapseSeconds, realElapseSeconds);
            if (m_ProcessTreasureCoroutine == null)
                m_ProcessTreasureCoroutine = StartCoroutine(ProcessTreasure());
        }

        private IEnumerator ProcessTreasure()
        {
            StartPanel.SetActive(true);
            yield return new WaitForSeconds(1f);
            StartPanel.SetActive(false);
            AnimatePanel.SetActive(true);
            GameEntry.Sound.PlaySound(EnumSound.TreasureBoxMusic, null);
            for (int i = 0; i < uIGameOverFormOpenParam.TreasureBoxData.MoneyInfos.Count; i++)
            {
                treasureProItem.gameObject.SetActive(false);
                var MoneyInfo = uIGameOverFormOpenParam.TreasureBoxData.MoneyInfos[i];
                MoneyEntityData worldMoneyData = GameEntry.Data.GetData<MoneyEntity>().GetMoneyEntityData(MoneyInfo.MoneyId);
                treasureProItem.SetItem(worldMoneyData.Icon, worldMoneyData.Name, MoneyInfo.Count);
                treasureProItem.gameObject.SetActive(true);
                yield return new WaitForSeconds(1f);
            }
            yield return new WaitForSeconds(1f);
            AnimatePanel.SetActive(false);
            EndPanel.SetActive(true);
            for (int i = 0; i < uIGameOverFormOpenParam.TreasureBoxData.MoneyInfos.Count; i++)
            {
                var CardInfo = uIGameOverFormOpenParam.TreasureBoxData.MoneyInfos[i];
                MoneyEntityData worldMoneyData = GameEntry.Data.GetData<MoneyEntity>().GetMoneyEntityData(CardInfo.MoneyId);
                var card = Instantiate(CardPrefab, EndCardNode);
                card.SetActive(true);
                card.GetComponent<TreasureProItem>().SetItem(worldMoneyData.Icon, worldMoneyData.Name, CardInfo.Count);
            }
        }

        protected override void OnClose(bool isShutdown, object userData)
        {
            var dataPlayer = GameEntry.Data.GetData<DataPlayer>();
            if (dataPlayer != null)
            {
                dataPlayer.GetBoxAward(uIGameOverFormOpenParam.TreasureBoxData.MoneyInfos, CurrencyEvent.LevelTreasureChest);
            }
            if (m_ProcessTreasureCoroutine != null)
            {
                StopCoroutine(m_ProcessTreasureCoroutine);
                m_ProcessTreasureCoroutine = null;
            }
            ResetInfo();
            base.OnClose(isShutdown, userData);
        }

        private void CloseForm()
        {
            Close();
            GameEntry.Event.Fire(this, StartWaveEventArgs.Create());
        }

        private void ResetInfo()
        {
            StartPanel.SetActive(false);
            AnimatePanel.SetActive(false);
            EndPanel.SetActive(false);

            for (int i = 0; i < EndCardNode.childCount; i++)
                Destroy(EndCardNode.GetChild(i).gameObject);
        }
    }
}
